/*****************************************************************************
 * ui_sys_settings.c: Copyright 2023 (c) CoreLink
 * Desc:
 *
 * Author: Flying (weekend@111.com)
 * Date: 2023-09-07 16:09:26
 * Notes:
 *
 * ---------------------------------------------------------------------------
 * LastEditors:
 * LastEditTime:
 *****************************************************************************/

/*------------------------------- Includes ----------------------------------*/
#include "ui_sys_settings.h"
#include "ui_common.h"

#include <stdio.h>
#include "tdl_display_manage.h"

/*------------------- 全局定义和声明 -------------------*/

/*---------------------- 常量/宏定义 -----------------------*/

/*----------------------- 类型声明 ---------------------------------*/

/*----------------------- 变量声明 -----------------------------*/

/*----------------------- 函数原型 --------------------------------*/
static lv_obj_t *ui_sys_settings_create_mem(ui_sys_settings_info_t *info, lv_obj_t *parent, const char *text);
static lv_obj_t *ui_sys_settings_create_temperature(ui_sys_settings_info_t *info, lv_obj_t *parent, const char *text);
static lv_obj_t *ui_sys_settings_create_cpu(ui_sys_settings_info_t *info, lv_obj_t *parent, const char *text, int32_t val);
static lv_obj_t *ui_sys_settings_create_slider(lv_obj_t *parent, const char *icon, const char *txt, int32_t min, int32_t max, int32_t val);
static lv_obj_t *ui_sys_settings_create_text(lv_obj_t *parent, const char *icon, const char *txt);
static void ui_sys_settings_temp_timer_cb(lv_timer_t *timer);
static void ui_sys_settings_mem_timer_cb(lv_timer_t *timer);
static void ui_sys_settings_cpu_timer_cb(lv_timer_t *timer);
static void ui_sys_settings_brightness_event_handler(lv_event_t *e);
static void ui_sys_settings_body_create(ui_sys_settings_info_t *info);
static void ui_sys_settings_open_changer_anim_cb(void *var, int32_t v);
static void ui_sys_settings_back_event_handler(lv_event_t *e);

/*----------------------- 功能实现 --------------------------------*/
ui_sys_settings_info_t *ui_sys_settings_create(lv_obj_t *parent)
{
    ui_sys_settings_info_t *info = (ui_sys_settings_info_t *)MY_MALLOC(sizeof(ui_sys_settings_info_t));
    memset(info, 0, sizeof(ui_sys_settings_info_t));
    info->parent = parent;
    tdl_disp_set_brightness(tdl_disp_find_dev(DISPLAY_NAME), 50);

    return info;
}

int ui_sys_settings_show(ui_sys_settings_info_t *info)
{
    if (info->cont)
    {
        return -1;
    }

    info->cont = lv_menu_create(info->parent);
    lv_obj_align(info->cont, LV_ALIGN_BOTTOM_MID, 0, 0);
    // lv_obj_set_size(info->cont, MY_UI_W_MAX, MY_UI_H_MAX);
    lv_obj_set_size(info->cont, 10, 10);
    lv_obj_set_style_bg_opa(info->cont, LV_OPA_0, 0);
    lv_obj_set_style_radius(info->cont, 0, 0);
    lv_obj_set_style_bg_opa(info->cont, LV_OPA_0, LV_PART_SCROLLBAR);
    lv_menu_set_mode_root_back_btn(info->cont, LV_MENU_ROOT_BACK_BTN_ENABLED);
    lv_obj_add_event_cb(info->cont, ui_sys_settings_back_event_handler, LV_EVENT_CLICKED, info);
    lv_obj_set_style_text_font(lv_menu_get_main_header(info->cont), ui_font_get_font(12 * MY_UI_H_ZOOM), 0);

    ui_sys_settings_body_create(info);

    lv_anim_t a;
    lv_anim_init(&a);
    lv_anim_set_var(&a, info->cont);
    lv_anim_set_exec_cb(&a, ui_sys_settings_open_changer_anim_cb);
    lv_anim_set_values(&a, 0, 256);
    lv_anim_set_time(&a, 200);
    lv_anim_start(&a);
    return 0;
}

int ui_sys_settings_delete(ui_sys_settings_info_t *info)
{
    return -1;
}

int ui_sys_settings_exit(ui_sys_settings_info_t *info)
{
    if (info->cpu_timer)
    {
        lv_timer_del(info->cpu_timer);
        info->cpu_timer = NULL;
    }

    if (info->mem_timer)
    {
        lv_timer_del(info->mem_timer);
        info->mem_timer = NULL;
    }

    if (info->temp_timer)
    {
        lv_timer_del(info->temp_timer);
        info->temp_timer = NULL;
    }

    if (info->cont)
    {
        lv_obj_del(info->cont);
        info->cont = NULL;
        return true;
    }
    return false;
}

/**
 * @description:
 * @param {lv_event_t *} e
 * @return {*}
 */
static void ui_sys_settings_back_event_handler(lv_event_t *e)
{
    lv_obj_t *obj = lv_event_get_target(e);
    ui_sys_settings_info_t *info = (ui_sys_settings_info_t *)lv_event_get_user_data(e);
    lv_obj_t *menu = info->cont;

    if (lv_menu_back_btn_is_root(menu, obj))
    {
        ui_sys_settings_exit(info);
    }
}

/**
 * @description: 动画轨迹
 * @param {void} *var
 * @param {int32_t} v
 * @return {*}
 */
static void ui_sys_settings_open_changer_anim_cb(void *var, int32_t v)
{
    lv_obj_t *obj = (lv_obj_t *)var;
    lv_coord_t w, h;

    w = lv_map(v, 0, 256, 10, MY_UI_W_MAX);
    h = lv_map(v, 0, 256, 10, MY_UI_H_MAX);
    lv_obj_set_size(obj, w, h);
    if (v > 250)
    {
        lv_obj_align(obj, LV_ALIGN_CENTER, 0, 0);
        lv_obj_set_style_bg_opa(obj, LV_OPA_100, LV_PART_SCROLLBAR);
    }
    else
    {
        lv_obj_align(obj, LV_ALIGN_BOTTOM_MID, 0, 0);
    }
    lv_opa_t o;
    o = lv_map(v, 0, 256, 0, LV_OPA_100 * 0.95);
    lv_obj_set_style_bg_opa(obj, o, 0);
}

static void ui_sys_settings_body_create(ui_sys_settings_info_t *info)
{
    lv_obj_t *section;

    lv_obj_t *sub_display_page = lv_menu_page_create(info->cont, NULL);
    lv_obj_set_style_pad_hor(sub_display_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(info->cont), 0), 0);
    lv_menu_separator_create(sub_display_page);
    section = lv_menu_section_create(sub_display_page);
    // lv_obj_t *slider = ui_sys_settings_create_slider(section, NULL, "屏幕亮度", 0, 100, luminance_get_brightness());
    lv_obj_t *slider = ui_sys_settings_create_slider(section, NULL, "屏幕亮度", 0, 100, 50);
    lv_obj_add_event_cb(slider, ui_sys_settings_brightness_event_handler, LV_EVENT_VALUE_CHANGED, NULL);

    // CPU 子页面
    lv_obj_t *cpu_sub_display_page = lv_menu_page_create(info->cont, NULL);
    lv_obj_set_style_pad_hor(cpu_sub_display_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(info->cont), 0), 0);
    lv_menu_separator_create(cpu_sub_display_page);
    section = lv_menu_section_create(cpu_sub_display_page);
    // float usage = settings_sys_info_get_cpu_usage();
    float usage = 100.0f;
    int32_t usage_val = (int32_t)usage;
    ui_sys_settings_create_cpu(info, section, "CPU", usage_val);
    info->cpu_timer = lv_timer_create(ui_sys_settings_cpu_timer_cb, 1000, info);

    // 内存 子页面
    lv_obj_t *mem_sub_display_page = lv_menu_page_create(info->cont, NULL);
    lv_obj_set_style_pad_hor(mem_sub_display_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(info->cont), 0), 0);
    lv_menu_separator_create(mem_sub_display_page);
    section = lv_menu_section_create(mem_sub_display_page);
    ui_sys_settings_create_mem(info, section, "内存");
    info->mem_timer = lv_timer_create(ui_sys_settings_mem_timer_cb, 1000, info);

    // 温度 子页面
    lv_obj_t *temperature_sub_display_page = lv_menu_page_create(info->cont, NULL);
    lv_obj_set_style_pad_hor(temperature_sub_display_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(info->cont), 0), 0);
    lv_menu_separator_create(temperature_sub_display_page);
    section = lv_menu_section_create(temperature_sub_display_page);
    ui_sys_settings_create_temperature(info, section, "芯片温度");
    info->temp_timer = lv_timer_create(ui_sys_settings_temp_timer_cb, 1000, info);

    lv_obj_t *sub_todo = lv_menu_page_create(info->cont, NULL);
    lv_obj_set_style_pad_hor(sub_todo, lv_obj_get_style_pad_left(lv_menu_get_main_header(info->cont), 0), 0);
    lv_menu_separator_create(sub_todo);
    section = lv_menu_section_create(sub_todo);
    ui_sys_settings_create_text(sub_todo, LV_SYMBOL_SETTINGS, "TODO");

    lv_obj_t *item;
    lv_obj_t *root_page = lv_menu_page_create(info->cont, NULL);

    ui_sys_settings_create_text(root_page, LV_SYMBOL_SETTINGS, "设置");
    lv_obj_set_style_pad_hor(root_page, lv_obj_get_style_pad_left(lv_menu_get_main_header(info->cont), 0), 0);
    section = lv_menu_section_create(root_page);

    item = ui_sys_settings_create_text(section, NULL, "显示");
    lv_menu_set_load_page_event(info->cont, item, sub_display_page);
    item = ui_sys_settings_create_text(section, NULL, "屏保设置");
    lv_menu_set_load_page_event(info->cont, item, sub_todo);
    item = ui_sys_settings_create_text(section, LV_SYMBOL_WIFI, "WLAN设置");
    lv_menu_set_load_page_event(info->cont, item, sub_todo);

    ui_sys_settings_create_text(root_page, NULL, "监视器");
    section = lv_menu_section_create(root_page);
    item = ui_sys_settings_create_text(section, LV_SYMBOL_SETTINGS, "CPU");
    lv_menu_set_load_page_event(info->cont, item, cpu_sub_display_page);
    item = ui_sys_settings_create_text(section, LV_SYMBOL_SETTINGS, "内存");
    lv_menu_set_load_page_event(info->cont, item, mem_sub_display_page);
    item = ui_sys_settings_create_text(section, LV_SYMBOL_SETTINGS, "芯片温度");
    lv_menu_set_load_page_event(info->cont, item, temperature_sub_display_page);
    // item = ui_sys_settings_create_text(section, LV_SYMBOL_SETTINGS, "ROM");  //删掉
    // lv_menu_set_load_page_event(info->cont, item, sub_todo);

    lv_menu_set_sidebar_page(info->cont, root_page);

    lv_event_send(lv_obj_get_child(lv_obj_get_child(lv_menu_get_cur_sidebar_page(info->cont), 1), 0), LV_EVENT_CLICKED, NULL);
}

/**
 * @description:
 * @param {lv_event_t *} e
 * @return {*}
 */
static void ui_sys_settings_brightness_event_handler(lv_event_t *e)
{
    lv_obj_t *obj = lv_event_get_target(e);
    int val = lv_slider_get_value(obj);
    val = lv_map(val, 0, 100, 10, 100);
    tdl_disp_set_brightness(tdl_disp_find_dev(DISPLAY_NAME), val);
}

static void ui_sys_settings_cpu_timer_cb(lv_timer_t *timer)
{
}

static void ui_sys_settings_mem_timer_cb(lv_timer_t *timer)
{
}

static void ui_sys_settings_temp_timer_cb(lv_timer_t *timer)
{
}

static lv_obj_t *ui_sys_settings_create_text(lv_obj_t *parent, const char *icon, const char *txt)
{
    lv_obj_t *obj = lv_menu_cont_create(parent);

    lv_obj_t *img = NULL;
    lv_obj_t *label = NULL;

    if (icon)
    {
        img = lv_img_create(obj);
        lv_img_set_src(img, icon);
    }

    if (txt)
    {
        label = lv_label_create(obj);
        lv_obj_set_style_text_font(label, ui_font_get_font(12 * MY_UI_H_ZOOM), 0);
        lv_obj_set_style_pad_all(label, 0, 0);
        lv_obj_set_style_pad_top(label, -5 * MY_UI_H_ZOOM, 0);
        lv_label_set_text(label, txt);
        lv_label_set_long_mode(label, LV_LABEL_LONG_SCROLL_CIRCULAR);
        lv_obj_set_flex_grow(label, 1);
    }

    return obj;
}

static lv_obj_t *ui_sys_settings_create_slider(lv_obj_t *parent, const char *icon, const char *txt, int32_t min, int32_t max, int32_t val)
{
    lv_obj_t *obj = lv_menu_cont_create(parent);

    lv_obj_t *img = NULL;
    lv_obj_t *label = NULL;

    if (icon)
    {
        img = lv_img_create(obj);
        lv_img_set_src(img, icon);
    }

    if (txt)
    {
        label = lv_label_create(obj);
        lv_obj_set_style_text_font(label, ui_font_get_font(12 * MY_UI_H_ZOOM), 0);
        lv_obj_set_style_pad_all(label, 0, 0);
        lv_obj_set_style_pad_top(label, -5 * MY_UI_H_ZOOM, 0);
        lv_label_set_text(label, txt);
        lv_label_set_long_mode(label, LV_LABEL_LONG_SCROLL_CIRCULAR);
        lv_obj_set_flex_grow(label, 1);
    }

    if (icon && txt)
    {
        lv_obj_add_flag(img, LV_OBJ_FLAG_FLEX_IN_NEW_TRACK);
        lv_obj_swap(img, label);
    }

    lv_obj_t *slider = lv_slider_create(obj);
    lv_obj_set_flex_grow(slider, 1);
    lv_slider_set_range(slider, min, max);
    lv_slider_set_value(slider, val, LV_ANIM_OFF);

    if (icon == NULL)
    {
        lv_obj_add_flag(slider, LV_OBJ_FLAG_FLEX_IN_NEW_TRACK);
    }

    return slider;
}

static lv_obj_t *ui_sys_settings_create_cpu(ui_sys_settings_info_t *info, lv_obj_t *parent, const char *text, int32_t val)
{

    return NULL;
}

void ui_sys_settings_bar_event_cb(lv_event_t *e)
{
}
static lv_obj_t *ui_sys_settings_create_mem(ui_sys_settings_info_t *info, lv_obj_t *parent, const char *text)
{
    return NULL;
}

static lv_obj_t *ui_sys_settings_create_temperature(ui_sys_settings_info_t *info, lv_obj_t *parent, const char *text)
{
    return NULL;
}

/*---------------------------------------------------------------------------*/